// Created by lifei on 2020/7/18--8:35.
<template>
  <div class="indexMain">
    <el-row class="top mainItem">
      <el-col :span="14">
        <div class="content">
          <el-button type="primary" icon="el-icon-user-solid" circle />
          <div class="words">
            <div class="welcome">{{this.user.user.realName}}，欢迎登录！</div>
            <span>你的联系电话:{{this.user.user.phone}} | 所属部门(单位):{{this.user.user.deptName}}</span>
          </div>
        </div>
      </el-col>
    </el-row>

    <div class="middle mainItem">
      <div class="middle-item">
        <div class="title">我的消息</div>
        <ul class="list">
          <li v-for="item in myMessage" :key="item.msgId" @click="handleClickMyMessage(item.msgId)">
            {{item.messageHeader}}
            <img :src="newIcon" class="imgIcon" v-if="item.smsStatus == '0'" />
          </li>
        </ul>
      </div>
      <div class="middle-item">
        <div class="title">我的待办</div>
        <ul class="list">
          <li v-for="item in todoList" :key="item.busiId" @click="todoSkip(item)">{{item.busiType}}</li>
        </ul>
      </div>
      <div class="middle-item right">
        <el-tabs v-model="activeTab" class="dashboardTab">
          <el-tab-pane :label="tabLabel[name]" :name="name" v-for="(val,name) in contractRemindData" :key="name">
            <el-row class="totalList">
              <el-col :span="12" v-for="(item, index) in val" :key="index">
                <div class="panelContent" @click="toContractList(item)">
                  <a href="#">
                    <h3>{{item.num}}</h3>
                    <p>{{item.title}}</p>
                  </a>
                </div>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>

    <el-row class="bottom">
      <el-col :span="24">
        <div class="content">
          <div class="title">核心数据</div>
          <el-row class="totalList" v-for="(item,index) in coreData" :key="index">
            <el-col :span="6">
              <div class="panelContent">
                <h3>
                  {{(item.monthReceivable === '' ||item.monthReceivable === null
                  ||item.monthReceivable === undefined) ? 0 : item.monthReceivable}}
                </h3>
                <p>本月应收(元)</p>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="panelContent">
                <h3>
                  {{(item.monthActual === '' ||item.monthActual === null ||item.monthActual ===
                  undefined) ? 0 : item.monthActual}}
                </h3>
                <p>本月实收(元)</p>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="panelContent">
                <h3>
                  {{(item.arrearsMoney === '' ||item.arrearsMoney === null ||item.arrearsMoney ===
                  undefined) ? 0 : item.arrearsMoney}}
                </h3>
                <p>欠款金额(元)</p>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="panelContent">
                <h3>
                  {{(item.arrearsCount === '' ||item.arrearsCount === null ||item.arrearsCount ===
                  undefined) ? 0 : item.arrearsCount}}
                </h3>
                <p>欠款笔数（笔）</p>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import dbl from '@/utils/dbl'
import { tokenBase } from '~/admin/api/user'
import {
  getFinanceList,
  getContractRemind,
  getTodoList,
  getBudgetApply,
} from './api/dashboard'
import { messagePage } from '../message-center/message-list/api/message'
import BaseLoad from '@/assets/vue-mixin/BaseLoad'
import axios from '@/plugins/axios'
import { mapState } from 'vuex'

export default {
  name: 'dashboard-index',
  components: {},
  data () {
    return {
      //核心数据
      coreData: [],
      // 合同数据
      contractRemindData: {
        '1': [],
        '2': [],
        '3': [],
      },
      //我的消息
      myMessage: [],
      // 我的待办
      todoList: [],
      // 合同tab切换选中值
      activeTab: '1',
    }
  },
  computed: {
    ...mapState(['user']),
    //我的消息new图标
    newIcon: () => require('@/assets/images/new.png'),
    // 合同切换label
    tabLabel () {
      return { '1': '租赁', '2': '其他合同', '3': '外租物业', }
    }
  },
  mounted () {
    //财务数据
    this.handleGetFinanceList()

    //合同提醒
    this.handleContractRemind()

    // hxf-0922-我的消息请求
    this.handleGetListData()

    //获取待办列表
    this.getTodoList()
  },
  methods: {
    // 获取待办事项
    getTodoList () {
      // getTodoList().then((res) => {
      //   if (res.data.code == 0) {
      //     this.todoList = res.data.data.records
      //   }
      // })
    },
    // 待办跳转办理事件
    todoSkip (data) {
      axios
        .get('/api/flowbusimain/getIsLock/' + data.busiId)
        .then((response) => {
          if (response.data.msg == null) {
            // 锁定任务
            this.doTaskLock(data.busiId)

            switch (data.busiTypeCode) {
              case '009':
                this.$router.push({
                  path: '/wy-flow/daiban-list2',
                  query: { busiIds: data },
                })
                break
              case '001':
                // 待受理 编辑页面 审批不通过
                ;['1000', '1202'].includes(data.busiState) &&
                  this.$router.push({
                    path: '/wy-flow/daiban-budget-apply2',
                    query: { busiIds: data },
                  })

                // 待审批 预算申请业务逻辑
                data.busiState === '1200' &&
                  getBudgetApply(data.busiId).then((respData) => {
                    this.$router.push({
                      path:
                        respData.data.data.isEdit == '1'
                          ? '/wy-flow/daiban-budget-check2'
                          : '/wy-flow/daiban-budget-check',
                      query: { busiIds: data },
                    })
                  })
                break
              case '004':
                // 待受理 编辑页面 // 审批不通过
                ;['1000', '1202'].includes(data.busiState) &&
                  this.$router.push({
                    path: '/wy-flow/daiban-budget-amend',
                    query: { busiIds: data },
                  })

                // 待审批 预算申请业务逻辑
                data.busiState == '1200' &&
                  getBudgetApply(data.busiId).then((respData) => {
                    this.$router.push({
                      path: '/wy-flow/daiban-budget-amendcheck',
                      query: { busiIds: data },
                    })
                  })
                break
              case '005':
                // 审批不通过
                if (data.busiState == '1000') {
                  this.$router.push({
                    path: '/wy-flow/daiban-budgetadd-edit',
                    query: { busiIds: data },
                  })
                }

                // 待审批
                data.busiState == '1200' &&
                  getBudgetApply(data.busiId).then((respData) => {
                    this.$router.push({
                      path:
                        respData.data.data.isEdit == '1'
                          ? '/wy-flow/daiban-budgetadd-check2'
                          : '/wy-flow/daiban-budgetadd-check',
                      query: { busiIds: data },
                    })
                  })

                // 审批不通过
                data.busiState == '1202' &&
                  this.$router.push({
                    path: '/wy-flow/daiban-budgetadd-edit',
                    query: { busiIds: data },
                  })
                break
              default:
                break
            }
          } else {
            this.$message.error(response.data.msg)
          }
        })
    },
    // 锁定待办任务
    doTaskLock (busiId) {
      const config = { headers: { Authorization: dbl.get('token') } }
      axios
        .get('/api/flowbusimain/doTaskLock/' + busiId, config)
        .then((response) => {
          console.log(response.data)
        })
    },
    // hxf-0916-核心数据请求接口getFinanceList
    handleGetFinanceList () {
      // getFinanceList().then((res) => {
      //   if (res.data.code === 0) {
      //     this.coreData = res.data.data
      //   }
      // })
    },
    // hxf-0922-我的消息请求
    handleGetListData () {
      this.loadAction('数据加载中')
      messagePage({ current: 1, size: 7 })
        .then((res) => {
          this.myMessage = res.data.data.data.records.slice(0, 7)
          this.loadClose()
        })
        .catch((e) => {
          this.loadClose()
        })
    },
    // hxf-0925-我的消息点击跳转详情页
    handleClickMyMessage (msgId) {
      this.$router.push({ name: 'message_details', query: { 'msgId': msgId } })
    },
    // 获取租赁合同核心数据
    handleContractRemind () {
      // getContractRemind().then((response) => {
      //   if (response.data.code == 0) {
      //     let contractRemindData = { '1': [], '2': [], '3': [] }
      //     response.data.data.forEach(item => {
      //       ['1', '2', '3'].includes(item.type) && contractRemindData[item.type].push(item)
      //     })
      //     this.contractRemindData = contractRemindData
      //   }
      // })
    },
    // 跳转合同
    toContractList ({ type, title }) {
      const warningType = new Map([
        ['即将到期合同', '01'],
        ['待续租合同', '02'],
        ['本月新签合同', '03'],
        ['本月终止合同', '04'],
        ['外租待续租合同', '02'],
        // ['当前执行合同', '01'],
      ]).get(title)
      const moduleType = {
        '1': '/wy-rent/contract/list',
        '2': '/wy-operate/collection/contract',
        '3': '/wy-lessor/contract/list',
      }[type]
      if (!type || !moduleType) return;
      if (!this.user.roles.rentcontract_get) return this.$message.warning('暂无合同管理权限');
      let skipUrl = `${moduleType}${type === '1' && title === '本月终止合同' ? '-history' : ''}?warningType=${warningType}`
      this.$router.push(skipUrl)
    },

    // 跳转合同
    toContractListHis (warningType) {
      this.user.roles.rentcontract_get
        ? this.$router.push({
          path: '/wy-rent/contract/list-history',
          query: { warningType },
        })
        : this.$message.warning('暂无合同管理权限')
    },

    toContractListLessor (warningType) {
      this.user.roles.lessorcontract_get
        ? this.$router.push({
          path: '/wy-lessor/contract/list',
          query: { warningType },
        })
        : this.$message.warning('暂无合同管理权限')
    },
  },
  mixins: [BaseLoad],
}
</script>

<style scoped lang='scss'>
@import "./index.scss";
</style>
